 ********************************************************************************
**	PURPOSE: Treatment effects by baseline installment borrowing, create AT4, RLT1
**							
**	INPUTS: Wide with outcomes.dta
**	
**	OUTPUTS: het_takeup.xlsx - Panel A
**			 HET effects by OI.csv - Panel B	
**			 	
**  CREATED/EDITED BY: Kayla Wilding, Leah Kim, Hasan Ahamed
**
**	DATE CREATED: 7/2018
**
**	DATE LAST EDITED: 3/1/2023
********************************************************************************

clear all

/*******************************************************************************
1. Panel A
********************************************************************************/

*locals
local sample1_het ltca
local sample2_het mtca
local sample3_het htca

local decs 3

local testvars_het age1 female1 married1 adults1 child1 race_black1 college1 z_risk_i1 ///
z_contknow_i1 z_liqcf_i1 z_default_index_all_i1 binindex1 scoredf1 ficoscore081 z_credac_i1  ///
z_credac_rev_i1 prevloans1

local spec_het "ltca mtca htca"
local test_het "ltca = mtca = htca"

local end_het 3

use "$adta/Wide with outcomes.dta", clear

keep if flag_randomized == 1 // keep those who were part of the study --> there are people who said not interested who ended up taking the loan
*generate indicator variable for opening account for CBL and extra arm
gen op18cbl = op18 if flag_cblgroup==1 
gen op18extra = op18 if flag_extragroup==1 

matrix define T1 = J(2,4,.)
forval t = 1/3 {
	qui sum op18cbl if `sample`t'_het'== 1
	matrix T1[1,`t'] = r(mean)
	matrix T1[2,`t'] = r(sd)
}

reg op18cbl `spec_het', nocons
test `test_het'
matrix T1[1,4] = r(p)

matrix define T2 = J(2,4,.)
forval t = 1/3 {
	qui sum op18extra if `sample`t'_het'== 1
	matrix T2[1,`t'] = r(mean)
	matrix T2[2,`t'] = r(sd)
}

reg op18extra `spec_het', nocons
test `test_het'
matrix T2[1,4] = r(p)

keep if op18 == 1 // keep those who took-up across both arms

foreach suf in het {

local ordercount 1
foreach var in `testvars_`suf'' {	
	gen order`var'= `ordercount'
		forval x=1/`end_`suf'' {
			*summary statistics		
			ttest `var' if  `sample`x'_`suf'' == 1, by(enc)

			*store the mean and sd 
			gen mu`x'0`var' = r(mu_1)
			gen sd`x'0`var' = r(sd_1)	
					
			*store the mean and sd 
			gen mu`x'1`var' = r(mu_2)
			gen sd`x'1`var' = r(sd_2)	
			
			gen pval`x'`var' = r(p)	
		}

	local ++ordercount
}

*keep only variables of interest (mean and sd)
keep mu?* sd?* pval* order*
*keep only one observation for each mean and sd 
duplicates drop

gen i=1
ds i, not
reshape long mu10 mu11 sd10 sd11 mu20 mu21 sd20 sd21 mu30 mu31 sd30 sd31 order pval1 pval2 pval3, i(i) j(credvar) string

*format the variables - set the decimal numbers and add parenthesis to the standard errors
	ds i order credvar, not 
	foreach var in `r(varlist)' {
		gen `var'2 = string(`var', "%14.`decs'f")
		drop `var'
		rename `var'2 `var'
	}
	ds sd*
	foreach var in `r(varlist)' {
		replace `var' = "(" + `var' + ")"
	}

expand 2
sort order
forval i=1/`end_`suf'' {
replace mu`i'0 = sd`i'0 if credvar == credvar[_n-1] 
replace mu`i'1 = sd`i'1 if credvar == credvar[_n-1] 
}
drop sd*
drop i order

export excel "$outputtables/`suf'_takeup.xlsx", replace firstrow(var)

}

loc row `: word count `testvars_het''

loc note "Unit of observation is an individual. Index variables are standardized to the Extra Step Arm; see Data Appendix for details on index components and construction. Sample is restricted to CBL arm or Extra-Step arm for the first two rows. Sample size varies across rest of the rows due to missing observations"

putexcel set "$outputtables/het_takeup.xlsx", modify
putexcel A`=`row'*2+3' = matrix(T1)
putexcel A`=`row'*2+6' = matrix(T2)
putexcel A`=`row'*2+9' = "`note'"

/*******************************************************************************
2. Panel B
********************************************************************************/
use "$adta/Long with outcomes.dta", clear
sort surveyid index
gen noopeninstallB1 = ~ openinstallB1
replace noopeninstallB1 = . if missing(openinstallB1)

*Fico score thresholds
gen grth_590 = (ficoscore08 >= 590) & ~missing(ficoscore08)
gen grth_620 = (ficoscore08 >= 620) & ~missing(ficoscore08)

local outcomes scoredf ficoscore08 grth_590 grth_620 
loc spec 1.post#1.openinstallB1 1.post#1.noopeninstallB1 1.enc#1.post#1.openinstallB1 1.enc#1.post#1.noopeninstallB1

foreach var of local outcomes { 
  
		areg `var' `spec' if `=cond("`var'" == "grth_590" | "`var'" == "grth_620" | "`var'" == "grth_660", "flag_randomized_scored_base", "flag_randomized")'  == 1, absorb(surveyid) vce(cluster surveyid) 
		est store `var'
		
		estadd scalar obs = e(N)
		estadd scalar indv = e(df_a) + 1
		sum `var' if index == 1 & enc == 0
		estadd scalar mean_extra = r(mean)

	    test 1.enc#1.post#1.openinstallB1 = 1.enc#1.post#1.noopeninstallB1	
		estadd scalar p1 = r(p)	
}

esttab scoredf ficoscore08 grth_590 grth_620 using "$outputtables/HET effects by OI.csv", b(%15.3f) se(%15.3f) nogaps nonum nostar stats(p1 obs indv mean_extra, fmt(%15.3f %9.0f %9.0f %15.3f )) nonotes nomtitles replace 

**EOF**

